package com.ly.algorithm.offerpointer;

/**
 * @Classname Offer36
 * @Description
 *
 * 输入一棵二叉搜索树，将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点，只能调整树中节点指针的指向。
 *
 *
 *
 * @Date 2021/1/30 13:05
 * @Author 冷心影翼
 */
public class Offer36 {

    public static void main(String[] args) {

    }
}

class Solution36 {

    Node head,pre;

    public Node treeToDoublyList(Node root) {
        if(root == null) {
            return null;
        }
        inorder(root);
        head.left = pre;
        pre.right = head;
        return head;
    }

    public void inorder(Node cur) {
        if(cur == null) {
            return;
        }
        inorder(cur.left);
        if(pre == null)  {
            head = cur;
        }else {
            pre.right = cur;
        }
        cur.left = pre;
        pre = cur;
        inorder(cur.right);
    }

    class Node {
        public int val;
        public Node left;
        public Node right;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val,Node _left,Node _right) {
            val = _val;
            left = _left;
            right = _right;
        }
    }
}

